import {Component,OnInit,ViewChild} from '@angular/core';
import { PageModel } from "../../../../model/page-model";
import { ModalDirective } from 'ng2-bootstrap/modal';
import { flyIn } from '../../../../animations/fly-in';
import { MyHttp } from '../../../../service/MyHttp';

@Component({
  selector: 'devices',
  templateUrl: './devices.component.html',
  styleUrls: ['./devices.component.css'],
  animations: [
    flyIn
  ]
  
})
export class Devices implements OnInit{
  devData:any[];
  @ViewChild('myModal') public myModal:ModalDirective;
  pageModel = new PageModel();
  page:number = 1;  
  locker_category:any;
  locker_address:any;
  locker_device_id:any;
  locker_channel_trader:any;
  locker_maintenance:any;
  btype:number;
  selectedId:any;
  productdata:any;
  product_id:any;

  constructor(private myHttp:MyHttp) {}
  ngOnInit() { 
    this.getdata();
    this.getproductdata();
  } 
  

  getdata(){    
    this.myHttp.post('locker/findAll',{}).subscribe(res=>{
      if(res.errorCode==0){
         this.devData = res.message;
         console.log(this.devData)
      }    
      
    })
  }

  savedata(){
    console.log(this.product_id);
    this.myHttp.post('locker/create',
    {
      "locker_category" : '',
      "locker_address" : this.locker_address||'',    
      "locker_device_id" : this.locker_device_id,
      "locker_channel_trader" : this.locker_channel_trader||'',
      "locker_maintenance" : this.locker_maintenance||'',
      "product_id" : this.product_id
    }).subscribe(res=>{
      if(res.errorCode==0){
        alert(res.message);
        this.myModal.hide();
        this.locker_category='';
        this.locker_address='';
        this.locker_device_id='';
        this.locker_channel_trader='';
        this.locker_maintenance='';
        this.getdata();
      }
    });
  }

  deleteModal(item){
    this.btype = 2;
    this.selectedId = item;
    this.myModal.show();
  }
  getproductdata(){
    this.myHttp.post('product/findAll',
    {
      
    }).subscribe(res=>{
      if(res.errorCode==0){ 
        this.productdata = res.message;
        //console.log(res)
      }
    });
  }

  deldata(){
    this.myHttp.post('locker/delete',
    {
      "locker_device_id" : this.selectedId  
    }).subscribe(res=>{
      if(res.errorCode==0){
        alert(res.message);
        this.myModal.hide();
        this.getdata();
      }
    });
  }
  

  cancel(){
    this.locker_category='';
     this.locker_address='';
      this.locker_device_id='';
       this.locker_channel_trader='';
        this.locker_maintenance='';
  }

  

}

